package com.example.demo.controller;

import com.example.demo.entity.Order;
import com.example.demo.entity.OrderVersion;
import com.example.demo.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 订单控制类s
 */
@RestController
@RequestMapping("/api/orders")
public class OrderController {

    private static final Logger logger = LoggerFactory.getLogger(OrderController.class);

    @Autowired
    private OrderService orderService;

    /**
     * 创建新订单
     */
    @PostMapping
    public ResponseEntity<Order> createOrder(@RequestBody Order order) {
        logger.info("创建订单");
        Order savedOrder = orderService.createOrder(order);
        logger.info("保存订单{}", savedOrder);
        return ResponseEntity.ok(savedOrder);
    }

    /**
     * 更新订单
     */
    @PutMapping("/{id}")
    public ResponseEntity<Order> updateOrder(@PathVariable Long id, @RequestBody Order updatedOrder) {
        logger.info("更新订单:{}", id);
        updatedOrder.setId( id);
        Order result = orderService.updateOrder(id, updatedOrder);
        if (result == null) {
            logger.info("订单不存在");
            return ResponseEntity.notFound().build();
        } else {
            logger.info("保存订单:{}", result);
            return ResponseEntity.ok(result);
        }
    }

    /**
     * 验证订单完整性
     *
     */
    @GetMapping("/verify/{orderNo}")
    public ResponseEntity<Boolean> verifyOrderIntegrity(@PathVariable Long orderNo) {
        logger.info("验证订单完整性:{}", orderNo);
        boolean verified = orderService.verifyOrderIntegrity(orderNo);
        return ResponseEntity.ok(verified);
    }

    /**
     * 获取订单详情
     *
      */
    @GetMapping("/{id}")
    public ResponseEntity<Order> getOrder(@PathVariable Long id) {
        logger.info("获取订单详情{}", id);
        Optional<Order> order = orderService.getOrderById(id);
        logger.info("订单详情{}", order);
        return order.map(ResponseEntity::ok).orElse(ResponseEntity.notFound().build());
    }

    /**
     * 获取订单的所有版本历史
     */
    @GetMapping("/{id}/versions")
    public ResponseEntity<List<OrderVersion>> getOrderVersions(@PathVariable Long id) {
        logger.info("获取订单的所有版本历史{}", id);
        List<OrderVersion> versions = orderService.getOrderVersions(id);
        if (versions != null) {
            logger.info("订单的所有版本历史{}", versions);
            return ResponseEntity.ok(versions);
        } else {
            logger.info("订单不存在");
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 获取订单的最新版本
     */
    @GetMapping("/{id}/latest-version")
    public ResponseEntity<OrderVersion> getLatestOrderVersion(@PathVariable Long id) {
        logger.info("获取订单的最新版本{}", id);
        Optional<OrderVersion> latestVersion = orderService.getLatestOrderVersion(id);
        logger.info("订单的最新版本{}", latestVersion);
        return latestVersion.map(ResponseEntity::ok).orElse(ResponseEntity.notFound().build());
    }

    /**
     * 测试创建示例订单
     */
    @PostMapping("/test")
    public ResponseEntity<Order> createTestOrder() {
        logger.info("创建示例订单");
        Order savedOrder = orderService.createTestOrder();
        logger.info("保存订单{}", savedOrder);
        return ResponseEntity.ok(savedOrder);
    }
}